;
! function() {
	var layer = layui.layer,
		form = layui.form;
		formSelects = layui.formSelects;

	// 当勾选不同意协议按钮后禁止注册
	form.on("checkbox(agreen-checkbox)", function(data) {
		if(!data.elem.checked) {
			$("#reg").addClass("layui-btn-disabled");
			$("#reg").prop("disabled", "disabled");
		} else {
			$("#reg").removeClass("layui-btn-disabled");
			$("#reg").removeAttr("disabled");
		}
	});

/*	// 验证码值存储变量
	var vailCode;
	// 执行获取验证码
	refCode();

	// 点击刷新验证码
	$("#refCode_reg_img").on("click", function() {
		refCode();
	});

	// 获取图片验证码
	function refCode() {
		$.ajax({
			url: "user/imageVailCode.do",
			type: "post",
			success: function(result) {
				vailCode = result.data.rand;
				$("#refCode_reg_img").prop("src", "data:image/jpg;base64," + result.data.image);
				$("#code").val("");
			}
		});
	}*/

	var account_status;

	// 验证账号唯一
	function vailAccount() {
		$.ajax({
			url: "/register/validate",
			type: "POST",
			async: false,
			data: {
				"username": $("#username").val()
			},
			success: function(result) {
				account_status = result.code;
			}
		});
	}

	// 自定义验证规则
	form.verify({
		username: function() {
			vailAccount();
			if(account_status != 0) {
				return "该账号已被注册";
			}
		},
		pwd: function(value) {
			if(value.length < 6) {
				return "密码长度不能少于6位";
			} else if(!/^(\w){6,20}$/.test(value)) {
				return "密码只能包含字母、数字或下划线";
			}
		},
		rePwd: function(value) {
			if(value != $("#password").val()) {
				return "两次输入的密码不一致";
			}
		},
		phone: [/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/, '手机号格式不对！'],
		email: [/^[a-z0-9._%-]+@([a-z0-9-]+\.)+[a-z]{2,4}$|^1[3|4|5|7|8]\d{9}$/, '邮箱格式不对']
	});

	//监听提交  
	form.on("submit(register)", function() {
		if(account_status != 0) {
			layer.msg("该账号已被注册", {
				icon: 5
			});
		} else {
			var serializeDiv = {};
			
			$('#registerForm [name]').each(function(){
				var $comp = $(this);
				
				if ($comp.is("select")) {
					serializeDiv[$comp.attr("name")] = formSelects.value('select3', 'valStr');
				} else {
					serializeDiv[$comp.attr("name")] = $comp.val();
				}
			});
			
			$.ajax({
				url: "/register",
				type: "POST",
				async: false,
				data: serializeDiv,
				success: function(result) {
					if(result.code == 0) {
						layer.msg("注册成功");
						setTimeout("location='/login'", 2000);
					} else {
						$("form")[0].reset();
						layer.msg(result.msg, {
							icon: 5
						});
					}
				}
			});
		}
		
		return false;
	});
	
    $.get("/register/option-dept-list", function(data) {
        var html = "";
        for (var i in data) {
            html += '<option value="' + data[i].deptId + '">' + data[i].deptId + "  " + data[i].name + '</option>';
        }
        
        $("#shopCode").append(html);
        form.render();
    }, "json");
}();